package com.banyuan.servlet.author;

import com.banyuan.dao.AuthorDao;
import com.banyuan.domain.Author;
import com.banyuan.utils.CommonUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * 处理注册
 */
@WebServlet("/author/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 去前端拿数据
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String nickName = req.getParameter("nickName");
        String imageCode = req.getParameter("imageCode");

        // 1.判断用户在前端输入的值是否为null或者""

        if (CommonUtils.isEmpty(userName) || CommonUtils.isEmpty(password) ||
                CommonUtils.isEmpty(repassword) || CommonUtils.isEmpty(nickName) ||
                CommonUtils.isEmpty(imageCode)) {

            req.setAttribute("msg","输入项为空"); //给前端传一句话
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            // 跳转到登陆页面
            return; // 阻断下面代码运行
        }



        // 2.判断验证码是否输入正确 ----通过session去拿验证码
        HttpSession session = req.getSession();
        String imagecode = (String)session.getAttribute("IMAGECODE");
        System.out.println("1");
        // equalsIgnoreCase 忽略大小写去比较相不相等
        if (!imageCode.equalsIgnoreCase(imagecode)) {
            req.setAttribute("msg","验证码不正确"); //给前端传一句话
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            // 跳转到登陆页面
            return; // 阻断下面代码运行
        }


        // 3.判断名字是否过长
        if (userName.length() > 30) {
            req.setAttribute("msg","用户太长，请重新取名"); //给前端传一句话
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            // 跳转到登陆页面
            return; // 阻断下面代码运行
        }

        // 4. 输入的两次密码是否相等
        if (!password.equals(repassword)) {
            req.setAttribute("msg","两次输入的密码不相等"); //给前端传一句话
            req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            // 跳转到登陆页面
            return; // 阻断下面代码运行
        }


        // 5.判断名字是否和数据库里的名字重复
        ResultSet resultSet = null;
        try {
             resultSet = AuthorDao.selectAuthorByUserName(userName);
             resultSet.next();
            int countName = resultSet.getInt("countName");
            // countName > 0 说明重复了
            if (countName > 0 ) {
                req.setAttribute("msg","用户名重复了"); //给前端传一句话
                req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
                // 跳转到登陆页面
                return; // 阻断下面代码运行
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }

        // 6.以上都没问题的话，将注册的author存储到数据库里，并且跳转到登陆页面

        Author author = new Author();
        author.setUserName(userName);
        // CommonUtils.calculateMD5 将密码加密
        author.setPassword(CommonUtils.calculateMD5(password));
        author.setNickName(nickName);
        int i = 0;
        try {
            i = AuthorDao.insert(author);

            if (i > 0 ) {
                req.getRequestDispatcher("/author/loginPage").forward(req,resp);
            } else {
                req.setAttribute("msg","注册失败，请重新注册"); //给前端传一句话
                req.getRequestDispatcher("/WEB-INF/pages/author/register.jsp").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }












    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
